package atguigu.timeutils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {
	/**
	 * 将时间转换为当前时区时间字符串
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static String convertDateToCurrentTimeZoneString(Date timestamp) {
		String resultTime = "";
		if(timestamp != null){
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			resultTime = dateFormat.format(timestamp);
		}
		return resultTime;
	}
	
	/**
	 * 将时间转换为当前时区时间字符串
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToDate(String dateString) throws ParseException {
		Date resultDate = null;
		if(!StringUtil.isEmpty(dateString)){
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			resultDate = dateFormat.parse(dateString);
		}
		return resultDate;
	}
	
	/**
	 * 将带有UTC的时间字符串进行转换
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToDateContainZ(String dateString) throws ParseException {
		Date resultDate = null;
		if(!StringUtil.isEmpty(dateString)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
			sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			resultDate = sdf.parse(dateString);
		}
		return resultDate;
	}
	
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static Date getCurrentDate() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        Date date = new Date(cal.getTime().getTime());
		return date;
	}
	
	/**
	 * 将时间转换为当前时区时间字符串
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static String convertTimeToCurrentTimeZoneString(Timestamp timestamp) {
		String resultTime = "";
		if(timestamp != null){
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			resultTime = dateFormat.format(timestamp);
		}
		return resultTime;
	}
	
	/**
	 * 将时间转换为当前时区时间字符串
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp convertStringToTime(String timeString) throws ParseException {
		Timestamp timestamp = null;
		if(!StringUtil.isEmpty(timeString)){
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			Date date = dateFormat.parse(timeString);
			timestamp = new Timestamp(date.getTime());
		}
		return timestamp;
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static Timestamp getCurrentTime() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        Timestamp date = new Timestamp(cal.getTime().getTime());
		return date;
	}
	
	/**
	 * 获取之前或之后的时间（时分秒以后有需要再添加）
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date getDateAfterOrBefor(Date date, int count) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date); 
		calendar.add(Calendar.DAY_OF_MONTH, count); //把日期往后增加一天,整数  往后推,负数往前移动 
		date = calendar.getTime();
		return date;
	}
	
	/*
	* 获取时间字符串
	*/
	public static String getCurrentStringTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        return sdf.format(date);
    }
	
	/*
	* 获取时间字符串
	*/
	public static Date getDateByFormat(String strData, String strFormat) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
      Date date = new Date();
      date = sdf.parse(strData);
      return date;
	}
	
	/*
	* 获取时间字符串
	*/
	public static String getStringByFormatAndDate(Date date, String strFormat) throws ParseException {
		String dateStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		dateStr = sdf.format(date);
		return dateStr;
	}
}
